package ExtendConfigUtils

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Slice"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
	"reflect"
)

func GetExtendConfigList(extendConfigEntityList map[string]commEntity.IEntity, entityType string) map[string]*edgeEntity.ExtendConfigEntity {
	if len(extendConfigEntityList) == 0 {
		return make(map[string]*edgeEntity.ExtendConfigEntity)
	}

	result := make(map[string]*edgeEntity.ExtendConfigEntity)
	for _, entity := range extendConfigEntityList {
		extendConfigEntity := entity.(*edgeEntity.ExtendConfigEntity)

		if entityType == "DeviceEntity" || entityType == "DeviceMapperEntity" {
			if extendConfigEntity.ExtendType == entityType+"Globe" {
				result["Globe"] = extendConfigEntity
				continue
			}
			if extendConfigEntity.ExtendType == entityType+"Type" {
				result["Type"] = extendConfigEntity
				continue
			}
			if extendConfigEntity.ExtendType == entityType+"Object" {
				result["Object"] = extendConfigEntity
				continue
			}
		}
	}

	return result
}

func ExtendMapList(mapList []map[string]interface{}, extendConfigEntityList map[string]commEntity.IEntity, entityType string) {
	entityMap := GetExtendConfigList(extendConfigEntityList, entityType)
	for _, data := range mapList {
		ExtendMap(data, entityMap)
	}
}

func ExtendMap(entity map[string]interface{}, extendMap map[string]*edgeEntity.ExtendConfigEntity) {
	if entity == nil || extendMap == nil {
		return
	}

	// 按优先级进行添加：存量参数 > Object缺省值 > Type缺省值 >Globe缺省值
	extendMapLevel(entity, extendMap, "Object")
	extendMapLevel(entity, extendMap, "Type")
	extendMapLevel(entity, extendMap, "Globe")
}

func extendMapLevel(entity interface{}, extendMap map[string]*edgeEntity.ExtendConfigEntity, level string) {
	if entity == nil || extendMap == nil {
		return
	}

	// 生成一个匿名函数：提取entity的extendParam
	getExtendParam := func(entity interface{}) map[string]interface{} {
		if reflect.TypeOf(entity).Kind() == reflect.Map {
			mp := entity.(map[string]interface{})
			extendParam := mp["extendParam"]
			return extendParam.(map[string]interface{})
		}
		if reflect.TypeOf(entity).Kind() == reflect.TypeOf(edgeEntity.Type.DeviceEntity).Kind() {
			extendParam := (entity.(*edgeEntity.DeviceEntity)).ExtendParam
			return extendParam
		}
		if reflect.TypeOf(entity).Kind() == reflect.TypeOf(edgeEntity.Type.DeviceMapperEntity).Kind() {
			extendParam := (entity.(*edgeEntity.DeviceMapperEntity)).ExtendParam
			return extendParam
		}

		return nil
	}

	// 执行匿名函数
	extendParam := getExtendParam(entity)
	if extendParam == nil {
		return
	}

	extendConfigEntity := extendMap[level]
	if extendConfigEntity == nil {
		return
	}

	// 设备对象级别
	if extendConfigEntity.ExtendType == "DeviceEntityObject" {
		// 定义一个匿名函数
		getDeviceName := func(entity interface{}) string {
			if reflect.TypeOf(entity).Kind() == reflect.Map {
				mp := entity.(map[string]interface{})
				if mp["deviceName"] != nil {
					return mp["deviceName"].(string)
				}
			} else if reflect.TypeOf(entity).Kind() == reflect.TypeOf(edgeEntity.Type.DeviceEntity).Kind() {
				return (entity.(*edgeEntity.DeviceEntity)).DeviceName
			}

			return ""
		}
		deviceName := getDeviceName(entity)
		if deviceName == "" {
			return
		}

		binds := extendConfigEntity.ExtendParam["binds"]
		if binds == nil {
			return
		}

		//检测：是否存在该数值
		if !Slice.Has(binds, deviceName) {
			return
		}

		fields := extendConfigEntity.ExtendParam["fields"]
		if fields == nil {
			return
		}
		extendField(extendParam, fields.([]interface{}))
		return
	}

	// 设备类型级别
	if extendConfigEntity.ExtendType == "DeviceEntityType" {
		// 定义一个匿名函数
		getDeviceType := func(entity interface{}) (string, string) {
			if reflect.TypeOf(entity).Kind() == reflect.Map {
				mp := entity.(map[string]interface{})
				manufacturer := mp["manufacturer"]
				deviceType := mp["deviceType"]
				if manufacturer == nil {
					manufacturer = ""
				}
				if deviceType == nil {
					deviceType = ""
				}

				return manufacturer.(string), deviceType.(string)
			} else if reflect.TypeOf(entity).Kind() == reflect.TypeOf(edgeEntity.Type.DeviceEntity).Kind() {
				deviceEntity := entity.(*edgeEntity.DeviceEntity)
				return deviceEntity.Manufacturer, deviceEntity.DeviceType
			}

			return "", ""
		}
		manufacturer, deviceType := getDeviceType(entity)
		if manufacturer == "" || deviceType == "" {
			return
		}

		binds := extendConfigEntity.ExtendParam["binds"]
		if binds == nil {
			return
		}

		bingList := binds.([]interface{})
		for _, val := range bingList {
			if reflect.TypeOf(val).Kind() != reflect.Map {
				continue
			}

			bind := val.(map[string]interface{})

			if manufacturer != bind["manufacturer"] {
				continue
			}
			if deviceType != bind["deviceType"] {
				continue
			}

			fields := extendConfigEntity.ExtendParam["fields"]
			if fields == nil {
				return
			}
			extendField(extendParam, fields.([]interface{}))
		}

		return
	}

	// 设备全局级别
	if extendConfigEntity.ExtendType == "DeviceEntityGlobe" {
		fields := extendConfigEntity.ExtendParam["fields"]
		if fields == nil {
			return
		}
		extendField(extendParam, fields.([]interface{}))
		return
	}

	// 设备类型级别
	if extendConfigEntity.ExtendType == "DeviceMapperEntityType" {
		// 定义一个匿名函数
		getDeviceType := func(entity interface{}) (string, string) {
			if reflect.TypeOf(entity).Kind() == reflect.Map {
				mp := entity.(map[string]interface{})
				manufacturer := mp["manufacturer"]
				deviceType := mp["deviceType"]
				if manufacturer == nil {
					manufacturer = ""
				}
				if deviceType == nil {
					deviceType = ""
				}

				return manufacturer.(string), deviceType.(string)
			} else if reflect.TypeOf(entity).Kind() == reflect.TypeOf(edgeEntity.Type.DeviceMapperEntity).Kind() {
				deviceEntity := entity.(*edgeEntity.DeviceMapperEntity)
				return deviceEntity.Manufacturer, deviceEntity.DeviceType
			}

			return "", ""
		}
		manufacturer, deviceType := getDeviceType(entity)
		if manufacturer == "" || deviceType == "" {
			return
		}

		binds := extendConfigEntity.ExtendParam["binds"]
		if binds == nil {
			return
		}

		bingList := binds.([]interface{})
		for _, val := range bingList {
			if reflect.TypeOf(val).Kind() != reflect.Map {
				continue
			}

			bind := val.(map[string]interface{})

			if manufacturer != bind["manufacturer"] {
				continue
			}
			if deviceType != bind["deviceType"] {
				continue
			}

			fields := extendConfigEntity.ExtendParam["fields"]
			if fields == nil {
				return
			}
			extendField(extendParam, fields.([]interface{}))
		}

		return
	}

	// 设备全局级别
	if extendConfigEntity.ExtendType == "DeviceMapperEntityGlobe" {
		fields := extendConfigEntity.ExtendParam["fields"]
		if fields == nil {
			return
		}
		extendField(extendParam, fields.([]interface{}))
		return
	}

}

func extendField(extendParam map[string]interface{}, fields []interface{}) {
	for _, field := range fields {
		data := field.(map[string]interface{})
		fieldName := data["fieldName"]
		defaultValue := data["defaultValue"]
		if fieldName == nil || defaultValue == nil {
			continue
		}

		extendParam[fieldName.(string)] = defaultValue
	}
}
